﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using QLHS2010.QLHS2010_BUS_WebService;

namespace QLHS2010
{
    public partial class QLHSForm : Form
    {
        LopDTO[] dsLop;
        HocSinhDTO[] dsHS;
        QLHS2010_BUS_WebServiceSoapClient ws = new QLHS2010_BUS_WebServiceSoapClient();
        
        public QLHSForm()
        {
            InitializeComponent();
        }

        private void reLoadData()
        {
            dsHS = ws.getDsHocSinh();
            dataGridHocSinh.DataSource = dsHS;
        }
        
        private void QLHSForm_Load(object sender, EventArgs e)
        {
            dsLop = ws.getDsLop();
            cmbLop.DisplayMember = "TenLop";
            cmbLop.ValueMember = "TenLop";
            cmbLop.DataSource = dsLop;

            reLoadData();
        }

        private void LayThongTinHS(ref HocSinhDTO hs)
        {
            hs.MaHS = txtMaHS.Text;
            hs.TenHS = txtHoTen.Text;
            hs.DiaChi = txtDiaChi.Text;
            hs.NgaySinh = dtpNgaySinh.Value;
           
            if (rbNam.Checked)
                hs.GioiTinh = rbNam.Text;
            else
                hs.GioiTinh = rbNu.Text;

            if (txtNamHoc.Text != "")
                hs.NamHoc = int.Parse(txtNamHoc.Text);
            else
                hs.NamHoc = 2007;

            hs.MaLop = cmbLop.SelectedValue.ToString();
        }
        
        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                HocSinhDTO hs = new HocSinhDTO();
                LayThongTinHS(ref hs);
                ws.insertHocSinh(hs);

                MessageBox.Show("Them THANH CONG!");
                reLoadData();
            }
            catch (Exception)
            {
                MessageBox.Show("Them THAT BAI!");
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                HocSinhDTO hs = new HocSinhDTO();
                LayThongTinHS(ref hs);
                ws.deleteHocSinh(hs);
                
                MessageBox.Show("Xoa THANH CONG!");
                reLoadData(); 
            }
            catch (Exception)
            {
                MessageBox.Show("Xoa THAT BAI!");
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                HocSinhDTO hs = new HocSinhDTO();
                LayThongTinHS(ref hs);
                ws.updateHocSinh(hs);

                MessageBox.Show("Sua THANH CONG!");
                reLoadData();
            }
            catch (Exception)
            {
                MessageBox.Show("Sua THAT BAI!");
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            reLoadData();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}